package mes.kanban.controller;

import java.io.Serializable;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import mes.kanban.entity.MesPermission;
import mes.kanban.entity.MesUser;
import mes.kanban.service.MesPermissionServiceI;
import mes.kanban.service.UserMessageServiceI;
import mes.kanban.util.PageBean;
import mes.kanban.vo.AjaxReturnVO;

/**
 * 用户信息管理controller
 * @author gjy
 *
 * 2018年2月6日下午4:31:35
 */

@Controller
@RequestMapping(value="/userMessage")
public class UserMessageController implements Serializable{

	private static final long serialVersionUID = 2131046413561732053L;
	
	private static final Logger log = LoggerFactory.getLogger(UserMessageController.class);
	
	@Autowired
	private UserMessageServiceI userMessageServiceI;
	
	@Autowired
	private MesPermissionServiceI permissionServiceI;
	
	@PostMapping(value="/saveUser")
	@ResponseBody
	public AjaxReturnVO registerUser(Model model,@RequestBody MesUser user) {
		
		AjaxReturnVO j = new AjaxReturnVO();
		
		try {
			user.setStatus(1);
			userMessageServiceI.saveUser(user);
			j.setOk(true);
			j.setMsg("注册成功，请登录");
		} catch (Exception e) {
			e.printStackTrace();
			j.setOk(false);
			j.setMsg(e.getMessage());
		}
		
		return j;
		
	}
	
	@GetMapping(value="/to_page")
	public String getUserList(HttpServletRequest req,Model model) {
		return "user/list";
	}
	
	@GetMapping(value="/getAllUsers")
	@ResponseBody
	public AjaxReturnVO getAllUsers(@RequestParam("pageNum")Integer pageNum,
				@RequestParam("pageSize")Integer pageSize) {
		
		AjaxReturnVO vo = new AjaxReturnVO();
		
		try {
			
			PageBean users = userMessageServiceI.findUserForPageBean(pageNum,pageSize);
			
			vo.setData(users);
			
			vo.setOk(true);
			
		} catch (Exception e) {
			
			e.printStackTrace();
			
			vo.setMsg("请求失败");
			
			vo.setOk(false);
		}
		return vo;
		
	}
	
	@PostMapping(value="/updateUser")
	@ResponseBody
	public AjaxReturnVO updateUser(@RequestBody MesUser user) {
		
		AjaxReturnVO vo = new AjaxReturnVO();
		
		try {
			vo.setOk(true);
			this.userMessageServiceI.updateUser(user);
		} catch (Exception e) {
			e.printStackTrace();
			vo.setOk(false);
		}
		
		return vo;
		
	}
	
	@GetMapping(value="/deleteUserById")
	@ResponseBody
	public AjaxReturnVO deleteUserById(@RequestParam("id")String id) {
	
		AjaxReturnVO vo = new AjaxReturnVO();
		
		try {
			vo.setOk(true);
			this.userMessageServiceI.deleteUserById(id);
		} catch (Exception e) {
			e.printStackTrace();
			vo.setOk(false);
			vo.setMsg("更新出错");
		}
		
		return vo;
	}
	
	@GetMapping(value="/to_menu_page")
	public String gotoRoleMange() {
		log.info("菜单管理界面");
		return "/user/menu";
	}
	
	@GetMapping(value="/getMenu")
	@ResponseBody
	public AjaxReturnVO getMenu(@RequestParam("pageNum")Integer pageNum,
			@RequestParam("pageSize")Integer pageSize) {
		
		AjaxReturnVO vo = new AjaxReturnVO();
		
		try {
			vo.setOk(true);
			
			PageBean pb = this.permissionServiceI.getPermissionsForBean(pageNum,pageSize);
			
			vo.setData(pb);
			
		} catch (Exception e) {
			e.printStackTrace();
			vo.setOk(false);
			vo.setMsg("报错了");
		}
		
		return vo;
	}
	
	@GetMapping(value="/deleteMenu")
	@ResponseBody
	public AjaxReturnVO deleteMenu(@RequestParam("id")String id) {
		AjaxReturnVO j = new AjaxReturnVO();
		try {
			
			j.setOk(true);
			
			this.permissionServiceI.deleteById(id);
			
		} catch (Exception e) {
			e.printStackTrace();
			j.setOk(false);
			j.setMsg("删除失败");
		}
		
		return j;
	}
	
	@PostMapping(value="/saveMenu")
	@ResponseBody
	public AjaxReturnVO saveMenu(@RequestBody MesPermission mesPermission) {
		
		AjaxReturnVO j = new AjaxReturnVO();
		
		try {
			
			j.setOk(true);
			
			this.permissionServiceI.savePermission(mesPermission);
			
		} catch (Exception e) {
			e.printStackTrace();
			j.setOk(false);
			j.setMsg("保存失败");
		}
		
		return j;
	}
	
}
